﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using GoogleMapsApi.Engine;
using GoogleMapsApi.Entities.Common;
using GoogleMapsApi.Entities.Geocoding.Request;
using GoogleMapsApi.Entities.Geocoding.Response;
using ProgramMain.Google;

namespace ProgramMain.GoogleSearch
{
    public class SearchResult
    {
         [XmlIgnore]
        public IEnumerable<AddressComponent> AddressComponents { get; set; }
        public string Address { get; set; }
        public Coordinate Coordinate { get; set; }
    }
    public class GoogleSearch
    {
        public static string Locality = "";
        private GeocodingRequest geocodeRequest;
    
        public GoogleSearch(string address)
        {
            geocodeRequest = new GeocodingRequest()
            {
                Address = address +" "+Locality,
                Language = "ru"
            };
        }

        public GoogleSearch(Coordinate coordinate)
        {
            string lat = coordinate.Latitude.ToString().Replace(",", ".");
            string lng = coordinate.Longitude.ToString().Replace(",", ".");
            geocodeRequest = new GeocodingRequest()
                                 {
                                     //   Location = new Location(coordinate.Latitude, coordinate.Longitude),
                                     Address = string.Format("{0}, {1}", lat,lng),
                                     Language = "ru"
                                 };
        }

        public List<SearchResult> Search()
        {
            GeocodingEngine geocodingEngine = new GeocodingEngine();
            List<SearchResult> res = new List<SearchResult>();
            GeocodingResponse geocode = geocodingEngine.GetGeocode(geocodeRequest);
            if (geocode != null && geocode.Results.Count() > 0)
            {
                foreach (Result result in geocode.Results)
                    res.Add(new SearchResult()
                                {
                                    AddressComponents = result.AddressComponents,
                                    Address = result.FormattedAddress,
                                    Coordinate =
                                        new Coordinate(result.Geometry.Location.Longitude,
                                                       result.Geometry.Location.Latitude)
                                });
            }

            return res;

        }

        public string AddressFormat(SearchResult address)
        {
            string res = "";

            string[] addressComponents = (from q in address.AddressComponents where q.Types.First() == "route" select q.LongName).ToArray();
                if (addressComponents.Length > 0)
                    res += addressComponents[0];
                
                addressComponents = (from q in address.AddressComponents where q.Types.First() == "street_number" select q.LongName).ToArray();
                if (addressComponents.Length > 0)
                    res += " " + addressComponents[0];

          //      addressComponents = (from q in address.AddressComponents where q.Types.First() == "point_of_interest" select q.LongName).ToArray();
         //       if (addressComponents.Length > 0)
                  
            return res.Trim();
        }
    }
}
